<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <button @click="fatherFn">按钮</button>
    <one></one>
    <two></two>
  </div>
  <script src="./vue.js"></script>
  <script>
    const bus = new Vue()  // 中介

    const vm = new Vue({
      el: '#app',
      methods: {
        fatherFn() {
          bus.$off('testfood')
        }
      },
      components: {
        one: {
          data() {
            return {
              food: '肉'
            }
          },
          template: `<div>
            <h2>我是one组件</h2>  
            <button @click="fn">传值 肉</button>
          </div>`,
          methods: {
            fn() {
              // emit 广播 传值出去
              bus.$emit('testfood', this.food)
            }
          }
        },
        two: {
          data() {
            return {
              res: ''
            }
          },
          mounted() {
            bus.$on('testfood', (res) => {  // 当监听到 testfood，就执行函数
              this.res = res
            })
          },
          template: `<div>
            <h2>我是two组件--{{res}}</h2>  
          </div>`
        }
      }
    })
  </script>
</body>
</html>